<template>
  <div class='xtx-goods-page'>
    <div class="container" v-if="goodData.categories">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <!-- 第一次去取的时候 数组还没拿到 所以goodData是空的  后面的就是undefined 所以用v-if
        goodData 有值再取出来 -->
        <XtxBreadItem :to='`/category/${goodData.categories[1].id}`'>{{goodData.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to='`/category/sub/${goodData.categories[0].id}`'>{{goodData.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem > {{goodData.name}} </XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
         <!-- 图片预览区 -->
          <div class="media">
            <!-- 商品图片 -->
            <goods-image :img-list='goodData.mainPictures'/>
            <!-- 商品评价和销量 -->
            <goods-sales :salesData='goodData'/>
          </div>
          <!-- 商品信息区 -->
          <div class="spec">
            <!-- 商品信息 -->
            <goods-name :good='goodData'/>
            <!-- sku -->
            <goods-sku
              :goods="goodData"
              @change="skuChange"
            ></goods-sku>
            <!-- 数量加减 -->
            <xtx-numbox v-model="num" :min='1' :max="10"></xtx-numbox>
            <!-- 加入购物车 -->
            <xtx-button type="primary" style="margin-top:20px;">加入购物车</xtx-button>
          </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <goods-tabs></goods-tabs>
          </div>
        </div>
        <!-- 24热榜 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, provide } from 'vue'
import { findGoods } from '@/api/good'
import { useRoute } from 'vue-router'
import GoodsImage from './components/GoodImage.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsSku from './Sku/index.vue'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsTabs,
    GoodsSku
  },
  setup () {
    const goodData = ref([]) // 商品详情列表
    const route = useRoute() // 创建路由实例 拿到上面的id
    const num = ref(1)

    async function loadGoodData () {
      const res = await findGoods(route.params.id)
      goodData.value = res.result
      console.log(res, '商品详情')
    }
    onMounted(() => {
      loadGoodData()
    })
    function skuChange (sku) {
      console.log(sku)
    }
    provide('goodData', goodData) // 不能写。value否则会失去响应式特性 拿到的就是一个值
    return {
      goodData,
      skuChange,
      num
    }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
      min-height: 600px;
      background: #fff;
      display: flex;
      .media {
        width: 580px;
        height: 600px;
        padding: 30px 50px;
      }
      .spec {
        flex: 1;
        padding: 30px 30px 30px 0;
      }
    }
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
